<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="视频信息" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-select v-model="listQuery.categoryId" placeholder="视频分类" clearable class="filter-item" style="width: 110px">
        <el-option :key="item.id" :value="item.id" v-for="item in category" :label="item.name" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="float: right;margin-right: 10px;" type="primary" icon="el-icon-rank" @click="addOrUpdateHandle()">新增</el-button>
    </div>

    <el-table v-loading="listLoading" :default-sort = "{prop: 'id', order: 'descending'}" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" label="视频ID" prop="id" width="50" sortable/>

      <el-table-column align="center" label="视频标题" min-width="100" prop="title"/>

      <el-table-column align="center" label="视频内容" min-width="100" prop="content"/>

      <el-table-column align="center" label="视频" width="400">
        <template slot-scope="scope">
<!--          <el-popover placement="left" trigger="hover">-->
<!--            <video width="80%" :src="scope.row.video" preload="auto" controls="controls" :poster="scope.row.cover"></video>-->
            <!--            <img :src="scope.row.video" width="300">-->
<!--            <img slot="reference" :src="scope.row.video" style="width: 70%">-->
          <div style="width: 100%">
            <video-player  class="video-player vjs-custom-skin"
                           ref="videoPlayer"
                           :playsinline="true"
                           :options="scope.row.playerOptions">
            </video-player>
          </div>
<!--          </el-popover>-->
        </template>
      </el-table-column>

<!--      <el-table-column align="center" label="视频封面" width="200">-->
<!--        <template slot-scope="scope">-->
<!--          <el-popover placement="left" trigger="hover">-->
<!--            <img :src="scope.row.cover" width="300" >-->
<!--            <img slot="reference" :src="scope.row.cover" style="width: 70%" preview preview-text="描述">-->
<!--          </el-popover>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column align="center" label="分类" width="100">
        <template slot-scope="scope">
          <el-tag >
            {{ scope.row.category.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :formatter="formatTime" width="102" align="center" sortable="custom" label="创建时间" prop="createTime"/>

      <el-table-column :formatter="formatTime" width="102" align="center" sortable="custom" label="修改时间" prop="updateTime"/>

      <el-table-column width="100" align="center" label="操作">
        <template slot-scope="scope">
          <el-row style="margin-bottom: 10px">
            <el-button size="mini" icon="el-icon-edit" type="primary" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          </el-row>
          <el-row>
            <el-button size="mini" icon="el-icon-delete" type="danger" @click="deleteHandle(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"/>
  </div>


</template>

<script>
import AddOrUpdate from './add-or-update'
import { putUser } from '@/api/user'
import { getAllCategory } from '@/api/picture'
import { getVideoList, deleteVideoById } from '@/api/video'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'
export default {
  name: 'Video',
  components: { AddOrUpdate, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      srcList:[],
      addOrUpdateVisible: false,
      category: [],
      listQuery: {
        current: 1,
        size: 10,
        ascs: undefined,
        descs: undefined,
      },
      // 视频播放
      playerOptions : []



    }
  },

  // created() {
  //   this.getList()
  // },
  methods: {
    getList() {
      this.listLoading = true
      getVideoList(this.listQuery).then(response => {
        if (response.data) {
          this.list = response.data.records
          this.total = response.data.total
          for (var index in response.data.records){
            // console.log(response.data.records[index].video)
            var playerOptions = {
              playbackRates : [ 0.5, 1.0, 1.5, 2.0 ], //可选择的播放速度
              autoplay : false, //如果true,浏览器准备好时开始回放。
              muted : false, // 默认情况下将会消除任何音频。
              loop : false, // 视频一结束就重新开始。
              preload : 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
              language : 'zh-CN',
              aspectRatio : '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
              fluid : true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
              sources : [ {
                type : "",
                src : response.data.records[index].video//url地址
              } ],
              poster : response.data.records[index].cover, //你的封面地址
              // width: document.documentElement.clientWidth,
              notSupportedMessage : '此视频暂无法播放，请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
              controlBar : {
                timeDivider : true,//当前时间和持续时间的分隔符
                durationDisplay : true,//显示持续时间
                remainingTimeDisplay : false,//是否显示剩余时间功能
                fullscreenToggle : true  //全屏按钮
              }
            }
            this.list[index].playerOptions = playerOptions
          }
          // console.log(response.data)
          // console.log(this.list)
        }
        this.listLoading = false
      })
    },
    // 排序
    sortChange(data) {
      if (data.order === 'ascending') {
        this.listQuery.descs = undefined
        this.listQuery.ascs = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      } else {
        this.listQuery.ascs = undefined
        this.listQuery.descs = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      }
      this.handleFilter()
    },
    // 搜索
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    // 格式化时间
    formatTime(row, column, cellValue) {
      return parseTime(cellValue)
    },
    // 禁用/启用用户
    updateUserStatus(id, isDisable) {
      this.$confirm('确定要' + (isDisable ? '禁用' : '启用') + '该用户么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        putUser({ id, isDisable }).then(response => {
          this.$message.success(response.msg)
          this.getList()
        })
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    //删除
    deleteHandle(id){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteVideoById(id).then(response => {
          this.$message.success(response.msg)
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //分类全部
    getAllCategory(){
      getAllCategory().then(res=>{
          // console.log("==============")
          this.category = res.data
          // console.log(this.category)
      }).catch(err=>{
        console.log(err)
      })
    }
  },
  mounted(){
    this.getAllCategory()
  }
}
</script>

<style scoped>
.filter-container {
    padding-bottom: 20px;
}
</style>
